<?php

namespace App\Models;

use App\Common\Constant\Params;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class CouponGrantList extends BaseModel
{
    protected $table = 'coupon_grantlist';

    protected $primaryKey = 'cg_id';

    const CREATED_AT = 'cg_add_time';
    const UPDATED_AT = null;

    protected $dateFormat = 'U';

    protected $fillable = [
        'cg_cid',
        'cg_meid',
        'cg_number',
        'cg_grantuser',
        'cg_status',
        'cg_starttime',
        'cg_endtime',
        'cg_use_time',
        'cg_user_order_number',
    ];

    protected $casts = [
        'cg_starttime'=>'datetime',
        'cg_endtime'=>'datetime',
        'cg_use_time'=>'datetime',
    ];

    public function coupon(){
        return $this->belongsTo(Coupon::class,'cg_cid');
    }

    public function merchant(){
        return $this->belongsTo(Merchant::class,'cg_meid');
    }

    /**
     * 使用状态文字
     * status_text
     */
    public function getStatusTextAttribute()
    {
        if (Carbon::now()->gt($this->cg_endtime)){
            return Params::$COUPON_STATUS[Params::COUPON_STATUS_EXPIRE];
        }else{
            return Params::$COUPON_STATUS[$this->cg_status];
        }

    }

    /**
     * 过期时间文字
     * vaildtime_text
     */
    public function getVaildtimeTextAttribute()
    {
        return $this->cg_starttime->format('Y/m/d').'-'.$this->cg_endtime->format('Y/m/d');
    }
}
